<?php
// Module Definition
$module['name'] = 'NSS-MySQL';
$module['description'] = 'Enable user@domain.tld formatted username logins.';
$module['depends'] = Array('unix_users');

switch($action) {
	case 'register':
		break;
	case 'unregister':
		break;
	case 'enable':
		global $cfg;
		$SQL = "CREATE TABLE IF NOT EXISTS user(
				user_id int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
				id bigint( 10 ) DEFAULT 0 NOT NULL ,
				username varchar( 255 ) DEFAULT '' NOT NULL ,
				user_name varchar( 255 ) DEFAULT '' NOT NULL ,
				realname varchar( 255 ) DEFAULT '' NOT NULL ,
				shell varchar( 20 ) DEFAULT '/bin/sh' NOT NULL ,
				password varchar( 40 ) DEFAULT '' NOT NULL ,
				status char( 1 ) DEFAULT 'N' NOT NULL ,
				uid int( 11 ) NOT NULL ,
				gid int( 11 ) DEFAULT '65534' NOT NULL ,
				homedir varchar( 32 ) DEFAULT '/bin/false' NOT NULL
			)";
		mysql_query($SQL);
	
		$SQL = 'SELECT domains.id, users.username, users.shell, users.name, DECODE(users.password,"'.$cfg['key'].'") AS password, users.root, domains.domain FROM users INNER JOIN domains ON users.id = domains.id WHERE users.type != "demo"';
		if ($res = mysql_query($SQL)) {
		if (mysql_num_rows($res)) {
			for($i=0; $row=mysql_fetch_array($res); $i++) {
				nss_users_create($row);
			}
		}	
		}
		break;
	case 'disable':
		$SQL = "DROP TABLE IF EXISTS nss_user";
		mysql_query($SQL);
		break;
	case 'hook':
		register_hook('server:user:create', 'nss_users_create', 1);
		register_hook('server:user:remove', 'nss_users_remove', 1);
		register_hook('server:user:update', 'nss_users_update', 1);
		register_hook('server:user:suspend', 'nss_users_suspend', 1);
		register_hook('server:user:unsuspend', 'nss_users_unsuspend', 1);
		break;
}
?>